package com.bawei.rk_01.Home

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.bawei.base.utils.SingLiveData
import com.bawei.base.utils.bean.CouponsEntity
import com.bawei.base.utils.bean.GoodEnttiy
import com.bawei.base.utils.bean.IndexEntity
import com.bawei.base.utils.bean.Res
import com.bawei.base.utils.mvvm.BaseViewModel
import kotlinx.coroutines.launch
import retrofit2.http.Query

class HomeViewModel : BaseViewModel() {

    //首页
    val indexsuccess = SingLiveData<Res<IndexEntity>>()
    val indexfail = SingLiveData<String>()
    fun index(){
        viewModelScope.launch {
            try {
                indexsuccess.postValue(apiService.index())
                Log.d("TAG", "index:$indexsuccess ")
            }catch (e:Exception){
                indexfail.postValue(e.message.toString())
            }
        }
    }

    //首页商品
    val goodssuccess = SingLiveData<Res<GoodEnttiy>>()
    val goodsfail = SingLiveData<String>()
    fun indexGoods(type:Int,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                goodssuccess.postValue(apiService.indexGoods(type,limit,page))
            }catch (e:Exception){
                goodsfail.postValue(e.message.toString())
            }
        }
    }


    //渲染优惠卷
    val couponssuccess = SingLiveData<Res<CouponsEntity>>()
    val couponsfail = SingLiveData<String>()
    fun getcoupons(limit:Int, page:Int, type:Int){
        viewModelScope.launch {
            try {
                couponssuccess.postValue(apiService.getcoupons(limit, page, type))
            }catch (e:Exception){
                couponsfail.postValue(e.message.toString())
            }
        }
    }

    //领取优惠卷
    val receivesuccess = SingLiveData<Res<String>>()
    val receivefail = SingLiveData<String>()
    fun receive(couponId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["couponId"] = couponId
                receivesuccess.postValue(apiService.receive(mapToBody(map)))
            }catch (e:Exception){
                receivefail.postValue(e.message.toString())
            }
        }
    }

}